/**
 * 2016年10月19日
 */
package jdk.effectivejava;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * @author Alex
 *
 */

public class Seriable   {
	static class A implements Serializable{
		public transient int data;
		public int field;
		public String toString(){
			return data+","+field;
		}
	}
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//序列化可以实现对象的复制等,super.clone()只是浅拷贝,序列化可以实现深拷贝
		OutputStream os = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(os);
		A a = new A();
		a.data = 5;
		a.field = 6;
		//1
		System.out.println(a);
		oos.writeObject(a);
		byte [] res = ((ByteArrayOutputStream)os).toByteArray();//res这个数组就可以表示一个A类的对象了
		os.close();
		ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(res));
		A cloned = (A)ois.readObject();
		//2
		System.out.println(new String(res));
		//3
		System.out.println(cloned);
		//4
		System.out.println(cloned==a);
	}

}
